package edu.corn.job.core.engine;

import edu.corn.job.core.context.JobRunnerLocal;
import edu.corn.job.core.context.JobRunningContext;
import edu.corn.job.core.processor.CallFallback;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 这个类负责包裹jobContext。同时处理回调方法。
 */
@AllArgsConstructor
@Data
public class JobRunner implements Runnable {

    private JobRunningContext context;


    @Override
    public void run() {

        JobRunnerLocal.setRunner(this);
        // 执行
        context.run();


        // 执行可能存在的回调。
        CallFallback callable = context.getCallable();
        if (callable != null) {
            if (context.getThrowable() != null) {
                callable.onFailure(context.getThrowable());
            } else {
                callable.onSuccess(context);
            }
        }
        JobRunnerLocal.removeRunner();
    }
}
